package cn.jji8.QSGUI.Gui;

import cn.jji8.QSGUI.Listener;
import org.bukkit.Bukkit;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.plugin.java.JavaPlugin;
import org.maxgamer.quickshop.shop.Shop;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

public class GuiManger extends Listener {

    //static
    public static void load(JavaPlugin main){
        Bukkit.getPluginManager().registerEvents(new GuiManger(),main);
    }
    static Map<Player,Gui> GuiMap = new HashMap<>();
    /**
     * 给玩家打开一个对应商店的Gui
     * */
    public static void open(Player player, Shop shop){
        Gui gui = Gui.getGui(shop);
        gui.open(player);
        GuiMap.put(player,gui);
    }
    /**
     * 重新给玩家打开当前商店gui
     * */
    public static void open(Player player){
        Gui gui = GuiMap.get(player);
        if(gui!=null){
            gui.open(player);
        }
    }

    //no static
    /**
     * 玩家点击物品栏时触发
     */
    @EventHandler
    public void InventoryCloseEvent(InventoryClickEvent a){
        Collection<Gui> list = GuiMap.values();
        Gui gui = GuiMap.get(a.getWhoClicked());
        if(gui!=null){
            gui.click(a);
        }
    }
    /**
     * 玩家关闭背包时触发
     * */
    @EventHandler
    public void InventoryCloseEvent(InventoryCloseEvent a){
        GuiMap.remove(a.getPlayer());
    }

}
